{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e443487f",
   "metadata": {},
   "source": [
    "# 实践：宿舍人脸识别门禁系统\n",
    "* 1. 创建FaceSet\n",
    "* 2. 查询FaceSet\n",
    "* 3. 存入face_token数据\n",
    "* 4. 获取FaceSet信息数据\n",
    "* 5. 人脸搜索\n",
    "* 6. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "575b2935",
   "metadata": {},
   "source": [
    "## 1. 创建人脸库 API\n",
    "* 描述\n",
    "* 1. 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。\n",
    "2. 试用API Key可以创建1000个FaceSet，正式API Key可以创建10000个FaceSet。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61b2d99c",
   "metadata": {},
   "source": [
    "* URL\n",
    "https://api-cn.faceplusplus.com/facepp/v3/faceset/create"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2bd38336",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f3dca9b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cf970c5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-0 准备工作\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "be5c51cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'U3ji7Jv4tkm3jr73ftNev7s2DX7E8Z8Q'\n",
    "API_S = 'bKI6FobIOCwjNSaFqKF0EdtZEbdpkAk2'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "94265fc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-1 创建人脸数据集合\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    \"api_key\":API_KEY,\n",
    "    \"api_secret\":API_S\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "41478c1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "45fe8b17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1b024bdc4e36beb788b821a315a6e03d',\n",
       " 'time_used': 186,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648364445,6313a877-5493-4d19-b3cb-dd2e730868b6',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "ee0d5e72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1b024bdc4e36beb788b821a315a6e03d'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()['faceset_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34722bda",
   "metadata": {},
   "source": [
    "# 2. 查询人脸库\n",
    "* 描述\n",
    "* 获取某一 API Key 下的 FaceSet 列表及其 faceset_token、outer_id、display_name 和 tags 等信息。\n",
    "* 注意：2017年8月16日后，调用本接口将不会一次性返回全量的 FaceSet。单次查询最多返回 100 个 FaceSet。如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序。\n",
    "## url\n",
    "https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\n",
    "* 调用方法\n",
    "POST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1f092b4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "66acd53e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>必选(二选一)</td>\n",
       "      <td>faceset_token</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet的标识</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>必选(二选一)</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>用户提供的FaceSet标识</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选（2017年8月16日起可使用）</td>\n",
       "      <td>start</td>\n",
       "      <td>Int</td>\n",
       "      <td>一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 是否必选            参数名      类型  \\\n",
       "0                  必选        api_key  String   \n",
       "1                  必选     api_secret  String   \n",
       "2             必选(二选一)  faceset_token  String   \n",
       "3             必选(二选一)       outer_id  String   \n",
       "4  可选（2017年8月16日起可使用）          start     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                     调用此API的API Key  \n",
       "1                                  调用此API的API Secret  \n",
       "2                                         FaceSet的标识  \n",
       "3                                     用户提供的FaceSet标识  \n",
       "4  一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是...  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Secret</span></p></td></tr><tr><td rowspan=\"2\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">必选(二选一)</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">faceset_token</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet的标识</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">用户提供的FaceSet标识</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p><p><span style=\"color: rgb(0,0,0);\">（2017年8月16日起可使用）</span></p></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">start</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是 [1,10000] 间的一个整数。 </span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">通过传入数字 n，可以控制本 API 从第 n 个 <span class=\"s1\">face_token </span>开始返回。返回的 face_token 按照创建时间排序，每次返回 <span class=\"s1\">100 </span>个 <span class=\"s1\">face_token</span>。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">默认值为 1。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">您可以输入上一次请求本 API 返回的 next 值，用以获得接下来的 100 个 face_token。</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "631ebf41",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请同学尝试书写 B-1 检查自己的数据集合是否创建成功\n",
    "# B-1\n",
    "getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':faceset_token\n",
    "}\n",
    "r = requests.post(url = getdetail_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5475aa93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "840d0173",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '88867adbfb96a47abb3aac61c56a22d1',\n",
       " 'tags': '',\n",
       " 'time_used': 91,\n",
       " 'user_data': '',\n",
       " 'display_name': '',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648364202,83bedcbc-6882-48af-8003-6633efdd6a38',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05a3c5c8",
   "metadata": {},
   "source": [
    "## 3.更新人脸库信息 API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "899ff920",
   "metadata": {},
   "outputs": [],
   "source": [
    "# C-1\n",
    "update_FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':faceset_token,\n",
    "    'new_outer_id':'2010130002',\n",
    "    'display_name':'2020级网新API周二上午班',\n",
    "    'user_data':'59人',\n",
    "    'tags':''\n",
    "}\n",
    "r = requests.post(url = update_FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "18617199",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "7c2b0c98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '1b024bdc4e36beb788b821a315a6e03d',\n",
       " 'request_id': '1648366497,32c2a08f-c9f1-410c-a0d0-e84b94ccb662',\n",
       " 'time_used': 95,\n",
       " 'outer_id': '2010130002'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f9a8d82",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "595bb3e5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90584519",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "163d1b67",
   "metadata": {},
   "source": [
    "## 4. 获取所有的FaceSet集合\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "efe2560e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# D-1\n",
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "a7aa7804",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "dae323a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 86,\n",
       " 'facesets': [{'faceset_token': '88867adbfb96a47abb3aac61c56a22d1',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '1b024bdc4e36beb788b821a315a6e03d',\n",
       "   'outer_id': 'API020305',\n",
       "   'display_name': '2020级网新API周二上午班',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648365657,f90973be-2dac-4ff5-92fa-e7647cfc7330'}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58fb671a",
   "metadata": {},
   "source": [
    "# 2. 实现所有人脸功能的应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c411600c",
   "metadata": {},
   "source": [
    "## 1.实现人脸检测获取face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0f509f3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b7d5c4cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\86150\\desktop\\python\\week04\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d17a3cdb",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'API_KEY' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-463eea8972bb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdetect\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mface_detect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAPI_KEY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mAPI_S\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'lkr.jpg'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'API_KEY' is not defined"
     ]
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'lkr.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "897c0740",
   "metadata": {},
   "source": [
    "# 3. 本周作业：\n",
    "尝试创建detect.py 并实现功能调用\n",
    "尝试创建analyze.py\\campare.py\\search.py，并实现功能调用\n",
    "尝试存放宿舍4位同学的人脸信息到 人脸数据集（XXX栋XXX宿舍"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b30023b2",
   "metadata": {},
   "source": [
    "# 尝试创建detect.py 并实现功能调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4f72cdfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_KEY = 'U3ji7Jv4tkm3jr73ftNev7s2DX7E8Z8Q'\n",
    "API_S = 'bKI6FobIOCwjNSaFqKF0EdtZEbdpkAk2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3f1c372f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "28202b75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\86150\\desktop\\python\\week04\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9f45002b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648369468,92eb3004-08ab-43d3-ba90-76bae51a4bf9',\n",
       " 'time_used': 809,\n",
       " 'faces': [{'face_token': 'b1fdbab07dc5873e175a639aafc66fcb',\n",
       "   'face_rectangle': {'top': 890, 'left': 776, 'width': 556, 'height': 556}}],\n",
       " 'image_id': '0xCGlJolgGByuwdOxiW7Dw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'lkr.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72ca57c7",
   "metadata": {},
   "source": [
    "# 尝试创建analyze.py，并实现功能调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "57aef532",
   "metadata": {},
   "outputs": [],
   "source": [
    "face1 = detect.face_detect(API_KEY,API_S,'lkr.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d5519fb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c6a8dcfdf7ea1163ae61d55c48399795'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens = face1['faces'][0]['face_token']\n",
    "face_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "70c33646",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8bd9f96f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_KEY, API_S, face_tokens, landmark=1, attributes=None, face_rectangle=None, beauty_score_min=0, beauty_score_max=100)\n",
      "        该函数为调用face++ face_analyze接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\86150\\desktop\\python\\week04\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7cbc8730",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 95,\n",
       " 'request_id': '1648370184,b05c0374-52a4-4d7b-b4ba-99b2dc024e2e',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 1252,\n",
       "     'x': 1048},\n",
       "    'mouth_upper_lip_top': {'y': 1237, 'x': 1108},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 1232, 'x': 1086},\n",
       "    'left_eye_upper_left_quarter': {'y': 958, 'x': 945},\n",
       "    'left_eyebrow_lower_middle': {'y': 921, 'x': 975},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 1269, 'x': 1064},\n",
       "    'right_eye_top': {'y': 963, 'x': 1222},\n",
       "    'left_eye_bottom': {'y': 994, 'x': 972},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 929, 'x': 1200},\n",
       "    'right_eye_pupil': {'y': 977, 'x': 1217},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 1287, 'x': 1141},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 1323, 'x': 1137},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 1307, 'x': 1158},\n",
       "    'contour_chin': {'y': 1448, 'x': 1106},\n",
       "    'contour_left9': {'y': 1445, 'x': 1034},\n",
       "    'left_eye_lower_right_quarter': {'y': 995, 'x': 1003},\n",
       "    'mouth_lower_lip_top': {'y': 1288, 'x': 1107},\n",
       "    'right_eyebrow_upper_middle': {'y': 899, 'x': 1231},\n",
       "    'left_eyebrow_left_corner': {'y': 926, 'x': 897},\n",
       "    'right_eye_bottom': {'y': 993, 'x': 1225},\n",
       "    'contour_left7': {'y': 1373, 'x': 910},\n",
       "    'contour_left6': {'y': 1321, 'x': 865},\n",
       "    'contour_left5': {'y': 1260, 'x': 833},\n",
       "    'contour_left4': {'y': 1194, 'x': 815},\n",
       "    'contour_left3': {'y': 1125, 'x': 808},\n",
       "    'contour_left2': {'y': 1058, 'x': 808},\n",
       "    'contour_left1': {'y': 990, 'x': 814},\n",
       "    'left_eye_lower_left_quarter': {'y': 987, 'x': 943},\n",
       "    'contour_right1': {'y': 991, 'x': 1305},\n",
       "    'contour_right3': {'y': 1105, 'x': 1302},\n",
       "    'contour_right2': {'y': 1047, 'x': 1307},\n",
       "    'mouth_left_corner': {'y': 1281, 'x': 1018},\n",
       "    'contour_right4': {'y': 1163, 'x': 1288},\n",
       "    'contour_right7': {'y': 1324, 'x': 1218},\n",
       "    'right_eyebrow_left_corner': {'y': 929, 'x': 1166},\n",
       "    'nose_right': {'y': 1155, 'x': 1181},\n",
       "    'nose_tip': {'y': 1141, 'x': 1140},\n",
       "    'contour_right5': {'y': 1217, 'x': 1267},\n",
       "    'nose_contour_lower_middle': {'y': 1184, 'x': 1125},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 922, 'x': 935},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 1322, 'x': 1070},\n",
       "    'right_eye_right_corner': {'y': 983, 'x': 1269},\n",
       "    'right_eye_lower_right_quarter': {'y': 989, 'x': 1249},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 1255, 'x': 1157},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 927, 'x': 1262},\n",
       "    'left_eye_left_corner': {'y': 975, 'x': 919},\n",
       "    'mouth_right_corner': {'y': 1285, 'x': 1171},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 1272, 'x': 1141},\n",
       "    'right_eye_lower_left_quarter': {'y': 993, 'x': 1199},\n",
       "    'left_eyebrow_right_corner': {'y': 928, 'x': 1060},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 926, 'x': 1017},\n",
       "    'right_eye_center': {'y': 983, 'x': 1223},\n",
       "    'nose_left': {'y': 1160, 'x': 1039},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 1285, 'x': 1063},\n",
       "    'left_eye_upper_right_quarter': {'y': 965, 'x': 1010},\n",
       "    'right_eyebrow_lower_middle': {'y': 926, 'x': 1232},\n",
       "    'left_eye_top': {'y': 953, 'x': 979},\n",
       "    'left_eye_center': {'y': 979, 'x': 975},\n",
       "    'contour_left8': {'y': 1416, 'x': 966},\n",
       "    'contour_right9': {'y': 1420, 'x': 1157},\n",
       "    'right_eye_left_corner': {'y': 993, 'x': 1174},\n",
       "    'mouth_lower_lip_bottom': {'y': 1328, 'x': 1106},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 895, 'x': 931},\n",
       "    'left_eye_pupil': {'y': 971, 'x': 976},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 905, 'x': 1195},\n",
       "    'contour_right8': {'y': 1374, 'x': 1190},\n",
       "    'right_eyebrow_right_corner': {'y': 930, 'x': 1289},\n",
       "    'right_eye_upper_left_quarter': {'y': 972, 'x': 1195},\n",
       "    'left_eyebrow_upper_middle': {'y': 888, 'x': 977},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 906, 'x': 1265},\n",
       "    'nose_contour_left1': {'y': 1001, 'x': 1071},\n",
       "    'nose_contour_left2': {'y': 1109, 'x': 1058},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 1233, 'x': 1130},\n",
       "    'nose_contour_right1': {'y': 1000, 'x': 1146},\n",
       "    'nose_contour_right2': {'y': 1107, 'x': 1167},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 1305, 'x': 1040},\n",
       "    'contour_right6': {'y': 1271, 'x': 1242},\n",
       "    'nose_contour_right3': {'y': 1174, 'x': 1157},\n",
       "    'nose_contour_left3': {'y': 1177, 'x': 1081},\n",
       "    'left_eye_right_corner': {'y': 992, 'x': 1030},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 897, 'x': 1023},\n",
       "    'right_eye_upper_right_quarter': {'y': 968, 'x': 1249},\n",
       "    'mouth_upper_lip_bottom': {'y': 1266, 'x': 1107}},\n",
       "   'face_rectangle': {'width': 556, 'top': 890, 'left': 776, 'height': 556},\n",
       "   'face_token': 'c6a8dcfdf7ea1163ae61d55c48399795'}]}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_KEY,API_S,face_tokens)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3424094",
   "metadata": {},
   "source": [
    "# 尝试创建compare.py，并实现功能调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "57d63832",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648370367,904bb8bf-3aab-411f-9419-5432b99c4c2b',\n",
       " 'time_used': 774,\n",
       " 'faces': [{'face_token': 'd9d8cae2c39004cee7780baf04bde7ec',\n",
       "   'face_rectangle': {'top': 890, 'left': 776, 'width': 556, 'height': 556}}],\n",
       " 'image_id': '0xCGlJolgGByuwdOxiW7Dw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'lkr.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fa5e5f3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c6a8dcfdf7ea1163ae61d55c48399795'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token1 = face1['faces'][0]['face_token']\n",
    "face_token1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c56d1a79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648370496,3359983f-c524-41d3-b1c5-d56a85305e8b',\n",
       " 'time_used': 246,\n",
       " 'faces': [{'face_token': 'be2889f744693ed7afbfddfd0a285c02',\n",
       "   'face_rectangle': {'top': 297, 'left': 179, 'width': 348, 'height': 348}}],\n",
       " 'image_id': 'y1687ngSkOomX3xdh7krQw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'lkr2.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b24b2aa1",
   "metadata": {},
   "outputs": [],
   "source": [
    "face2 = detect.face_detect(API_KEY,API_S,'lkr2.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1fc41685",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'9f785647e244a9c6581a312a8d1b2fdc'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token2 = face2['faces'][0]['face_token']\n",
    "face_token2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "5e6df5d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5f7bb4a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_KEY, API_S, face_token1, face_token2)\n",
      "        该函数为调用face++ face_compare接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\86150\\desktop\\python\\week04\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "849a2e78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648370579,7c209324-2fee-4fd8-a766-19c93c7c1f36',\n",
       " 'time_used': 666,\n",
       " 'confidence': 76.039,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_KEY,API_S,face_token1,face_token2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96190d61",
   "metadata": {},
   "source": [
    "# 尝试创建search.py，并实现功能调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6756699d",
   "metadata": {},
   "outputs": [],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "}\n",
    "r = requests.post(url = getfacesets_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "85f45834",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 82,\n",
       " 'facesets': [{'faceset_token': '88867adbfb96a47abb3aac61c56a22d1',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '1b024bdc4e36beb788b821a315a6e03d',\n",
       "   'outer_id': '2010130002',\n",
       "   'display_name': '2020级网新API周二上午班',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648370909,33409bb9-b7ae-4491-9dd2-a76cfe67f8ed'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a7078fae",
   "metadata": {},
   "outputs": [],
   "source": [
    "faceset_tokens = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "37205239",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'88867adbfb96a47abb3aac61c56a22d1'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = faceset_tokens['facesets'][0]['faceset_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ce787090",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648371175,2bcc181c-6b98-496b-b8ec-745b5426dae8',\n",
       " 'time_used': 229,\n",
       " 'faces': [{'face_token': 'e30095810b547d424fa36a723c8e1378',\n",
       "   'face_rectangle': {'top': 564, 'left': 315, 'width': 250, 'height': 250}}],\n",
       " 'image_id': 'psqmc7FLvNpF6ZI/M4YyAg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_S,'lkr3.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "2457f2d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "face3 = detect.face_detect(API_KEY,API_S,'lkr3.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "05afd447",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d667028bcc6d4dbbeea1c5dd97cc19fa'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = face3['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d423cffb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "99680a0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module search:\n",
      "\n",
      "NAME\n",
      "    search\n",
      "\n",
      "FUNCTIONS\n",
      "    face_search(API_KEY, API_S, faceset_token, face_token)\n",
      "        该函数为调用face++ face_search接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\86150\\desktop\\python\\week04\\search.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "27593511",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 76,\n",
       " 'error_message': 'EMPTY_FACESET',\n",
       " 'request_id': '1648371179,87e95ad6-d0d2-4d99-bf78-f16241f1c1c3'}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_KEY,API_S,faceset_token,face_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63e0e2db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
